﻿Public Class MainForm

    Private _DeserializeDockContent As Docking.DeserializeDockContent

    Private WithEvents _ErrorDisplayTimer As New Timer()

    Private WithEvents _DimToolWin As DimList_ToolWindow
    Private _ExecToolWin As Execution_ToolWindow
    Private _FlowToolWin As FlowList_ToolWindow
    Private WithEvents _FuncToolWin As FuncList_ToolWindow
    Private _SQLToolWin As SQL_ToolWindow

    Private WithEvents _MetisCore As New Core.MetisCore

    Private Sub DoLogin()
        Dim frmLogin As LoginForm

        frmLogin = New LoginForm()
        If frmLogin.ShowDialog() = Windows.Forms.DialogResult.OK Then
            _MetisCore.Connect(frmLogin.cmbSID.Text, frmLogin.txtUsername.Text, frmLogin.txtPassword.Text)
        End If
    End Sub

    Private Sub HandleConStateChanged(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles _MetisCore.ConnectionStateChange
        'MsgBox(_MetisCore.DBTables(0).TableName & " / " & _MetisCore.DBTables(0).FieldList.Count)
        If e.CurrentState = Data.ConnectionState.Closed Or e.CurrentState = Data.ConnectionState.Broken Then
            lblConnectionState.Text = My.Resources.ConnectionMsg_StatusDisconnected
        Else
            lblConnectionState.Text = My.Resources.ConnectionMsg_StatusConnected
        End If
    End Sub

    Public Sub ErrorDisplayHandle(ByVal Message As String) Handles _FuncToolWin.ErrorOccurred
        lblErrorDisplay.Text = Message
        lblErrorDisplay.Visible = True
        _ErrorDisplayTimer.Start()
    End Sub

    Public Sub OpenMetisEntity(ByVal Entity As Core.IMetisEntity)
        For Each win As Docking.DockContent In DockPanelHost.Documents
            If TypeOf win Is DimForm AndAlso DirectCast(win, DimForm).Dimension.Equals(Entity) Then
                win.Activate()
                Exit Sub
            ElseIf TypeOf win Is FuncForm AndAlso DirectCast(win, FuncForm).Function.Equals(Entity) Then
                win.Activate()
                Exit Sub
                'ElseIf TypeOf win Is FlowForm AndAlso DirectCast(win, FlowForm).Flow.Equals(Entity) Then
            End If
        Next

        If TypeOf Entity Is Core.Dimensions.MetisDimension Then
            Dim NewDimDocument = New DimForm(Entity)
            NewDimDocument.MdiParent = Me
            AddHandler NewDimDocument.ErrorOccurred, AddressOf Me.ErrorDisplayHandle
            NewDimDocument.Show(DockPanelHost, Docking.DockState.Document)
        ElseIf TypeOf Entity Is Core.Functions.MetisFunction Then
            Dim NewFuncDocument = New FuncForm(Entity)
            NewFuncDocument.MdiParent = Me
            AddHandler NewFuncDocument.ErrorOccurred, AddressOf Me.ErrorDisplayHandle
            NewFuncDocument.Show(DockPanelHost, Docking.DockState.Document)
        ElseIf TypeOf Entity Is Core.Flows.MetisFlow Then
            'Dim NewFlowDocument = New FlowForm(Entity)
            'NewFlowDocument.MdiParent = Me
            'AddHandler NewFlowDocument.ErrorOccurred, AddressOf Me.ErrorDisplayHandle
            'NewFlowDocument.Show(DockPanelHost, Docking.DockState.Document)
        End If

    End Sub

    Private Sub HandleErrorDisplayTimer() Handles _ErrorDisplayTimer.Tick
        lblErrorDisplay.Visible = False
        _ErrorDisplayTimer.Stop()
    End Sub


    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim Assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetAssembly(GetType(frmMain))
        'Dim xmlStream As System.IO.Stream = Assembly.GetManifestResourceStream("Metis.Resource.DockLayout.xml")
        'DockPanelHost.LoadFromXml(xmlStream, _DeserializeDockContent)
        'xmlStream.Close()
        _ErrorDisplayTimer.Enabled = False
        _ErrorDisplayTimer.Interval = 10000


        _DimToolWin = New DimList_ToolWindow()
        _DimToolWin.Show(DockPanelHost, Docking.DockState.DockLeftAutoHide)

        _FuncToolWin = New FuncList_ToolWindow()
        _FuncToolWin.Show(DockPanelHost, Docking.DockState.DockLeftAutoHide)

        _FlowToolWin = New FlowList_ToolWindow
        _FlowToolWin.Show(DockPanelHost, Docking.DockState.DockLeftAutoHide)

        _SQLToolWin = New SQL_ToolWindow
        _SQLToolWin.Show(DockPanelHost, Docking.DockState.DockBottomAutoHide)

        _ExecToolWin = New Execution_ToolWindow
        _ExecToolWin.Show(DockPanelHost, Docking.DockState.DockBottomAutoHide)

        'If FileIO.FileSystem.FileExists(My.Application.Info.DirectoryPath & My.Settings.DocPanel_LayoutXML) Then
        '    DockPanelHost.LoadFromXml(My.Application.Info.DirectoryPath & My.Settings.DocPanel_LayoutXML, _DeserializeDockContent)
        'End If


        _DimToolWin.Init(_MetisCore, Me)
        _FuncToolWin.Init(_MetisCore, Me)
        _FlowToolWin.Init(_MetisCore, Me)

        DoLogin()

    End Sub

    Private Sub frmMain_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        'Dim Assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetAssembly(GetType(frmMain))
        'Dim xmlStream As System.IO.Stream = Assembly.GetManifestResourceStream("Metis.Resource.DockLayout.xml")
        'DockPanelHost.SaveAsXml(xmlStream, System.Text.Encoding.UTF8)
        'xmlStream.Close()

        If DockPanelHost.Documents.Count > 0 Then
            For i As Integer = DockPanelHost.Documents.Count - 1 To 0
                If Not (TypeOf DockPanelHost.Documents(i) Is DimList_ToolWindow OrElse _
                        TypeOf DockPanelHost.Documents(i) Is FuncList_ToolWindow OrElse _
                        TypeOf DockPanelHost.Documents(i) Is FlowList_ToolWindow OrElse _
                        TypeOf DockPanelHost.Documents(i) Is SQL_ToolWindow OrElse _
                        TypeOf DockPanelHost.Documents(i) Is Execution_ToolWindow) Then
                    DirectCast(DockPanelHost.Documents(i), Docking.DockContent).Close()
                    If DirectCast(DockPanelHost.Documents(i), Docking.DockContent).Visible Then
                        e.Cancel = True
                        Exit Sub
                    End If
                End If
            Next
        End If

        DockPanelHost.SaveAsXml(My.Application.Info.DirectoryPath & My.Settings.DocPanel_LayoutXML)


    End Sub

    Private Sub miConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miConnect.Click
        DoLogin()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

End Class
